<?php
class User_model extends MY_Model {
	
  // var $table = 'team';
    var $lastname   = '';
    var $firstname = '';
    var $middlename = '';
    var $email;
    var $username;
    var $password;
    var $activated = 0;
    var $address;
    var $usertype = 'CLIENT';
    var $activation_key = '';
  	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    /*********************************************
     * $this->table() is the tablename
     * $this->setBaseValues($isInsert)--CreatedOn,UpdatedOn,createdBy,CreatedOn,valid
     * *******************************************
     */


   public function searchStaff($limit, $start,  $keyword) {
  		$this->db->or_like("firstname",$keyword);
  		$this->db->or_like("lastname",$keyword);
  		$this->db->or_like("middlename",$keyword);
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->table());
        //foreach($query->result() as $q){
        //	echo $q->id;
        //}
        
        return $query->result();
   }
    

    
   function findAllByUsertype($usertype){
   	    $this->db->where("usertype", $usertype);
        $query = $this->db->get($this->table());
        return $query->result();
   	
   }
    
   function hasAdminAccount() {
   		$this->db->limit(1);
   		$query = $this->db->get($this->table());
   		return ($query->num_rows() > 0) ? true : false;
   }

   function findByActivationKey($key) {
   		$this->db->where('activation_key',$key);
   		$query = $this->db->get($this->table());
   		return $query->result();
   }
   
   function findByUsername($username) {
   		$this->db->where('username',$username);
   		$query = $this->db->get($this->table());
   		return $query->result();
   }
   
   
    function updateClientAccountInfo()
	{/*
		$this->db->set('trackingNumber', $this->trackingNumber);
		$this->db->set('status', $this->status);
		$this->db->set('forwarder', $this->forwarder);
		*/
		$validateUser = $this->findByUsername($this->input->post('username',TRUE));
		if(count($validateUser) > 0 && $validateUser[0]->id != $this->session->userdata('client_id')){
			
			
		}else{
		    $this->db->set('username', $this->input->post('username',TRUE));
			$this->db->set('firstname', $this->input->post('firstname',TRUE));
			$this->db->set('middlename',$this->input->post('middlename',TRUE));
			$this->db->set('lastname', $this->input->post('lastname',TRUE));
			$this->db->set('email', $this->input->post('email',TRUE));
			$this->db->set('address', $this->input->post('address',TRUE));
			$this->db->where('id',$this->session->userdata('client_id'));
			$this->db->update($this->table());
		}
	}
	
	
	function changePassword($id , $newpassword ){
		$this->db->set('password' , $newpassword);
		$this->db->where('id' ,$id);
   		$this->db->update($this->table());
	}
   
}